<?php

header("Access-Control-Allow-Origin:*");
/*
  3.教师开始新的投票
  接口方式:http post
  接口:addVote.php
  返回数据:"ok" 告诉教师创建成功
  传入数据:{"voteTime":"1234566723","voteTitle":"投票标题","voteType":"1","voteOptions":["选项1","选项2","选项3"]}
  测试地址:http://192.168.16.115/openapi/om_html5/addVote.php?voteTime=1234566723&voteTitle=张三&voteType=1&voteOptions=["选项1","选项2","选项3"]
 */
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
@error_reporting(0);
@set_time_limit(0);
@set_magic_quotes_runtime(0);
@ini_set('memory_limit', '512M');
$whiteboard_config_array = array();
//ini_set("display_errors","On");
//error_reporting(E_ALL);
$data['voteTime'] = isset($_REQUEST['voteTime']) ? $_REQUEST['voteTime'] : '';
$data['voteTitle'] = isset($_REQUEST['voteTitle']) ? $_REQUEST['voteTitle'] : '';
$data['voteType'] = isset($_REQUEST['voteType']) ? $_REQUEST['voteType'] : '';
$data['voteOptions'] = isset($_REQUEST['voteOptions']) ? $_REQUEST['voteOptions'] : '';
$data['voteOptions_num'] = count($data['voteOptions']);
$roomId = isset($_REQUEST['roomId']) ? $_REQUEST['roomId'] : '';
$sn_group_id = isset($_REQUEST['sn_group_id']) ? $_REQUEST['sn_group_id'] : '';
if ($data['voteTime'] && $data['voteTitle'] && $data['voteType'] && $data['voteOptions']) {
    //DEBUG 解析白板配置文件
    $isp = isset($_REQUEST['isp']) && !empty($_REQUEST['isp']) ? $_REQUEST['isp'] : ''; //运营商编号 1 电信 2 移动 3 联通
    if (empty($isp)) {
        $isp = 1;
    }
    $whiteboard_config_array = parse_ini_file("../../config/room/config_whiteboard_" . $isp . ".ini", false);
    require '../../source/class/memory/memory_driver_redis_multi.php';
    $config_redis = array();
    $config_redis['master'] = array(
        array('host' => $whiteboard_config_array['MEMORY_REDIS_MASTER_SERVER'],
            'port' => $whiteboard_config_array['MEMORY_REDIS_MASTER_PORT'],
            'auth' => $whiteboard_config_array['MEMORY_REDIS_MASTER_AUTH'],
            'db' => $whiteboard_config_array['MEMORY_REDIS_MASTER_DB'],
            'pconnect' => $whiteboard_config_array['MEMORY_REDIS_MASTER_PCONNECT'],
            'timeout' => $whiteboard_config_array['MEMORY_REDIS_MASTER_TIMEOUT']
        )
    );
    $config_redis['slave'] = array(
        array('host' => $whiteboard_config_array['MEMORY_REDIS_SLAVE_SERVER'],
            'port' => $whiteboard_config_array['MEMORY_REDIS_MASTER_PORT'],
            'auth' => $whiteboard_config_array['MEMORY_REDIS_MASTER_AUTH'],
            'db' => $whiteboard_config_array['MEMORY_REDIS_MASTER_DB'],
            'pconnect' => $whiteboard_config_array['MEMORY_REDIS_MASTER_PCONNECT'],
            'timeout' => $whiteboard_config_array['MEMORY_REDIS_MASTER_TIMEOUT']
        )
    );
    //DEBUG 初始化redis
    $redis = RedisClient::getInstance($config_redis);
    $data_serialize = serialize($data);
    @file_put_contents('./logs/vote/vote_info_' . $data['voteTime'] . '.log', $data_serialize . PHP_EOL);
    //DEBUG 初始化redis
    $redis = RedisClient::getInstance($config_redis);
    $redis->set('vote_info_' . $data['voteTime'], $data_serialize);
    $redis->set('vote_option_num_' . $data['voteTime'], $data['voteOptions_num']);
    $redis->close();
    echo 'ok';
}
?>